var req = require('../../interfaceImpl.js');
var config = require('../../interfaceConfig.js');
var strAppCode = config.SmallApp;
var app = getApp();
var ReqMsgSign = "";
Page({
  data: {
    tel: '',
    code: '',
    btntxt: '获取验证码',
    sendflag: false,
    second: 60,
     isStop:false
  },
  onLoad: function (options) {
    //TODO 获取会员卡列表
    var that = this;
  },
  getCode: function () {
    var that = this;
    if (this.validatemobile()) {
      // 设置为已经发送
      // 开始计时
      // 发起请求/获取验证码
       that.setData({
            sendflag: true,
            second:60,
            isStop:false
          });
      that.countdown();
      var phone = that.data.tel;
          return req.GetModiPwdMsg( phone,app.globalData.token).then(res => {
            console.log(res)
            if (res.data.success) {
            //   that.countdown();
            //   that.setData({ sendflag: true })
            } 
            else {
                 that.setData({
            sendflag: false,
            isStop:true
          });
              wx.showModal({
              title: '发送失败',
              content: res.data.data,
              showCancel: false,
              success: function (res) {
              }
            })
            }
          })
    } else {
      return;
    }
  },
  newinput: function (e) {
    this.setData({ newpasd: e.detail.value })
  },
  // 保存手机号
  tobind: function () {
    console.log("点击");
    var that = this;
    var tel = that.data.tel
    var code = that.data.code;
    var pwd = that.data.newpasd
    if (this.validatemobile() && tel.length> 0 && code.length >0 && pwd >0) {
          wx.showLoading({
          title: '请稍候', mask: true
        });
      return req.ModiPwd(tel, code,pwd, app.globalData.token).then(res => {
            wx.hideLoading();
        if (res.data.success) {
          that.setData({ hasReq: true })
           wx.showModal({
                        //title: '提交成功',
                        content: "修改成功",
                        showCancel: false,
                        success: function (res) {
                            wx.navigateBack({ changed: false });
                        }
                    })
        } else {
          wx.showModal({
              title: '修改失败',
              content: res.data.data,
              showCancel: false,
              success: function (res) {
              }
            })
        }
      })
    } else {
      setTimeout(function () {
        wx.showToast({
          title: '信息不完整！',
          image: '/static/images/cuo.png',
          duration: 1500
        })
      }, 10)
    }
  },
  // 验证手机号格式
  validatemobile: function () {
    var mobile = this.data.tel;
    if (mobile.length == 0) {
      wx.showToast({
        title: '请输入手机号！',
        image: '/static/images/cuo.png',
        duration: 1500
      })
      return false;
    }
    if (mobile.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        image: '/static/images/cuo.png',
        duration: 1500
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      wx.showToast({
        title: '手机号格式错误！',
        image: '/static/images/cuo.png',
        duration: 1500
      })
      return false;
    }
    return true;
  },
  // 计时
  countdown: function () {
    var that = this;
        console.log(this.data. second);
    var second = that.data.second;
    if (second == 0||this.data.isStop) {
      // console.log("Time Out...");
      that.setData({
        sendflag: false,
        second: 60,
      });
      return;
    }
    var time = setTimeout(function () {
      that.setData({
        second: second - 1
      });
      that.countdown(that);
    }, 1000)
  },
  // 获取输入的手机号
  TelInput: function (e) {
    this.setData({
      tel: e.detail.value
    })
  },
  // 获取输入的验证码
  CodeInput: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
})